//
//  LGTWalletVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/9/3.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import ZHRefresh

class LGTWalletVC: LGTBaseVC, UITableViewDelegate, UITableViewDataSource{
    var tableView:UITableView?
    var moneyLabel:UILabel?
    var incomeLabel:UILabel?
    var dataArray:Array<Dictionary<String, Any>> = []
    var moneyInfo:Dictionary<String, Any> = [:]
    var page:Int = 1
    override func viewDidLoad() {
        super.viewDidLoad()
        //加载数据
        self.getData()
    }
    
    override func setUI() {
        super.setUI()
        self.setBavBarStyle(navBarHiden: false, title: "我的钱包", back: true)
        
        self.view.backgroundColor = UIColor.white
        
        let walletImageView:UIImageView = LGTViewCreateUtil.share.createImageView(image: "walletbackground")
        self.view.addSubview(walletImageView)
        walletImageView.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(12))
            make.right.equalTo(SizeFit(-12))
            make.height.equalTo(SizeFit(172))
            make.top.equalTo(NavBarH + SizeFit(9))
        }
        
        let label:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: 0, text: "资金明细")
        label.font = KBFont(size: SizeFitFloat(15))
        self.view.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(walletImageView.snp_bottomMargin).offset(SizeFit(21))
            make.height.equalTo(SizeFitFloat(16))
        }
        
        let canCashLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(15), text: "可提现余额")
        self.view.addSubview(canCashLabel)
        canCashLabel.snp.makeConstraints { (make) in
            make.height.equalTo(SizeFitFloat(16))
            make.top.equalTo(walletImageView).offset(SizeFitFloat(29))
            make.left.equalTo(SizeFitFloat(34))
        }
        
        self.moneyLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: 0)
        self.moneyLabel?.font = KBFont(size: 30)
        self.view.addSubview(self.moneyLabel!)
        self.moneyLabel!.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFitFloat(33))
            make.height.equalTo(SizeFitFloat(33))
            make.top.equalTo(walletImageView).offset(SizeFitFloat(61))
        }
        
        let incomeDescLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(12), text: "收入总金额")
        self.view.addSubview(incomeDescLabel)
        incomeDescLabel.snp.makeConstraints { (make) in
            make.height.equalTo(SizeFitFloat(12))
            make.top.equalTo(walletImageView).offset(SizeFitFloat(134))
            make.left.equalTo(SizeFitFloat(34))
        }
        
        self.incomeLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(16))
        self.view.addSubview(self.incomeLabel!)
        self.incomeLabel!.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFitFloat(33))
            make.height.equalTo(SizeFitFloat(16))
            make.top.equalTo(walletImageView).offset(SizeFitFloat(114))
        }
        
        let cashBtn:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: "提现", font: Font_Normal_16, color: UIColor.white, selectedColor: UIColor.white, corner: SizeFitFloat(14))
        self.view.addSubview(cashBtn)
        cashBtn.addTarget(self, action: #selector(cashAction), for: UIControl.Event.touchUpInside)
        cashBtn.snp.makeConstraints { (make) in
            make.top.equalTo(walletImageView).offset(SizeFitFloat(39))
            make.right.equalTo(SizeFitFloat(-51))
            make.width.equalTo(SizeFitFloat(63))
            make.height.equalTo(SizeFitFloat(28))
        }
        
        //定义渐变的颜色（从黄色渐变到橙色）
        let topColor = ColorUtil(color: 0xFF691C)
        let buttomColor = ColorUtil(color: 0xFF733C)
        let gradientColors = [topColor.cgColor, buttomColor.cgColor]

        //定义每种颜色所在的位置
        let gradientLocations:[NSNumber] = [0.0, 1.0]

        //创建CAGradientLayer对象并设置参数
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = gradientColors
        gradientLayer.locations = gradientLocations

        //设置其CAGradientLayer对象的frame，并插入view的layer
        gradientLayer.frame = self.view.frame
        cashBtn.layer.insertSublayer(gradientLayer, at: 0)
        
        let tableY:Double = NavBarH + SizeFit(228)
        let tableH:Double = Double(KScreenH) - tableY
        self.tableView = UITableView.init(frame: CGRect.init(x: 0.0, y: tableY, width: Double(KScreenW), height: tableH), style: UITableView.Style.plain)
        self.view.addSubview(self.tableView!)
        self.tableView?.backgroundColor = ViewBackColor
        self.tableView?.separatorStyle = UITableViewCell.SeparatorStyle.none
        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        self.tableView?.register(LGTWalletCell.classForCoder(), forCellReuseIdentifier: "cell")
        
        weak var weakSelf = self
        let header = ZHRefreshNormalHeader.headerWithRefreshing(block: {
            weakSelf?.page = 1
            weakSelf?.getData()
        })
        self.tableView?.header = header
        
        self.tableView?.footer = ZHRefreshBackNormalFooter.footerWithRefreshing(block: {
            weakSelf?.page += 1
            weakSelf?.getData()
        })
    }
    
    func getData(){
        weak var weakSelf = self
        LGTHttpUtil.httpUtil.requestData(url: Wallet_URL, param: ["page":self.page], showMessage: false, success: { (json) in
            weakSelf?.tableView?.header?.endRefreshing()
            weakSelf?.tableView?.footer?.endRefreshing()
            let data:Dictionary<String, Any> = json["data"] as? Dictionary<String, Any> ?? [:]
            let amount_list:Array<Dictionary<String, Any>> = data["amount_list"] as? Array<Dictionary<String, Any>> ?? []
            
            if weakSelf?.page == 1{
                weakSelf?.dataArray = amount_list
            }else{
                weakSelf?.dataArray.append(contentsOf: amount_list)
            }
            weakSelf?.updateInfo(info: data)
            
            if amount_list.count == 0{
                weakSelf?.tableView?.footer?.endRefreshingWithNoMoreData()
            }else{
                weakSelf?.tableView?.footer?.resetNoMoreData()
            }
            weakSelf?.tableView?.reloadData()
        }, failure: { (json) in
            weakSelf?.tableView?.header?.endRefreshing()
            weakSelf?.tableView?.footer?.endRefreshing()
        })
    }
    
    func updateInfo(info:Dictionary<String, Any>){
        let amount:Int = info["amount"] as? Int ?? 0
        self.moneyLabel?.text = String(format: "¥%.2f", Double(amount) / 100.0)
        let income:Int = info["income"] as? Int ?? 0
        self.incomeLabel?.text = String(format: "%.2f", Double(income) / 100.0)
    }
    
    @objc func cashAction(){
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return  self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:LGTWalletCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LGTWalletCell
        let info = self.dataArray[indexPath.row]
        cell.data = info
       return cell
    }

}
